package com.gg.registration.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gg.common.result.CommonResult;
import com.gg.common.result.ResultCodeEnum;
import com.gg.common.utils.AuthContextUtils;
import com.gg.registration.enums.OrderStatusEnum;
import com.gg.registration.model.order.OrderInfo;
import com.gg.registration.order.service.OrderService;
import com.gg.registration.vo.order.OrderCountQueryVo;
import com.gg.registration.vo.order.OrderQueryVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 订单接口
 * Author：  chenjunjia
 * Date：    2022/4/23 18:09
 * WeChat：  China_JoJo_
 */
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {

    @Resource
    private OrderService orderService;

    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public CommonResult submitOrder(@PathVariable String scheduleId, @PathVariable Long patientId) {
        Long orderId = orderService.saveOrder(scheduleId, patientId);
        return orderId==null ? new CommonResult(ResultCodeEnum.FAIL) : new CommonResult(ResultCodeEnum.SUCCESS,orderId);
    }

    @GetMapping("auth/getOrder/{orderId}")
    public CommonResult getOrder(@PathVariable("orderId") String orderId) {
        OrderInfo orderInfo = orderService.getOrder(orderId);
        System.out.println(orderInfo);
        return orderId==null ? new CommonResult(ResultCodeEnum.FAIL) : new CommonResult(ResultCodeEnum.SUCCESS,orderInfo);
    }

    /**
     * 订单列表（条件查询带分页）
     * @param page
     * @param limit
     * @param orderQueryVo
     * @param request 请求对象，用于获取header中的token信息
     * @return
     */
    @GetMapping("auth/list/{page}/{limit}")
    public CommonResult list(@PathVariable Long page,
                             @PathVariable Long limit,
                             OrderQueryVo orderQueryVo,
                             HttpServletRequest request) {
        /**
         * 利用 token 获取用户主键 id
         */
        orderQueryVo.setUserId(AuthContextUtils.getUserId(request));
        Page<OrderInfo> pageParam = new Page<>(page,limit);
        IPage<OrderInfo> pageModel = orderService.selectPage(pageParam,orderQueryVo);
        return pageModel==null ? new CommonResult(ResultCodeEnum.FAIL) : new CommonResult(ResultCodeEnum.SUCCESS,pageModel);
    }

    /**
     * 获取订单状态
     * @return
     */
    @GetMapping("auth/getStatusList")
    public CommonResult getStatusList() {
        return new CommonResult(ResultCodeEnum.SUCCESS,OrderStatusEnum.getStatusList());
    }

    /**
     * 取消预约并退款
     * @param orderId
     * @return
     * @throws Exception
     */
    @GetMapping("auth/cancelOrder/{orderId}")
    public CommonResult cancelOrder(@PathVariable("orderId") Long orderId) throws Exception {
        return new CommonResult(ResultCodeEnum.SUCCESS,orderService.cancelOrder(orderId));
    }

    /**
     * 订单统计 Echarts
     * 远程调用
     * @param orderCountQueryVo 条件对象
     * @return 返回一个map而不是commonresult，是因为方便取值，因为用远程调用
     */
    @PostMapping("inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderService.getCountMap(orderCountQueryVo);
    }
}
